<?php
error_reporting(E_ERROR | E_PARSE);
//test_house script script created by Robert Cox on August 1st 2011
//Commandline Usage: test_actiontracking.php commandline (debug)
//URL Usage: http://.../../test_actiontracking.php?web=1              <= normal mode
//      -or- http://.../../test_actiontracking.php?web=1&debug=1      <= debug mode

// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

if(isset($_GET['debug'])){
                $debug = TRUE;
                $GLOBALS["debug"] = TRUE;
        } else {
                unset($debug);
        }

if(isset($_GET['web'])){
                $web = TRUE;
                $GLOBALS["web"] = TRUE;
        } else {
                unset($web);
        }


if (isset($debug)) { 
	echo "<br>* * * * *  D E B U G = O N * * * * *<br>";
	if (isset($web)) { 
		echo "<br>* * * * *  W E B = O N  * * * * *<br>";}
}


$status = "pending";
$testname = "AdMeld Integration Test";
$testshortname = "admeldtest";
$testdesc = "Verify that AdMeld Integration is Working";
$testurl = "test_admeld.php";
$testtime = time();


$today = date("F j, Y, g:i a");
$hour = date("G");
$datestamp = date("Y-m-d");
$random = rand();
$Targeting="\"house\"";

$cookiefile = tempnam("/tmp", "CURLCOOKIE");

// setup command line args (commandline only)
if (isset($argv[1])) { $commandline = $argv[1]; unset($debug);} 

//
// Step 1: Make Initial Ad Request to the Canary Cluster
// http://crtldev.aimatch.com:60000/canary/getid/mid=[random]/external=admeld
//
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Step 1: Make Initial Ad Request to the Canary Cluster</b></font><br>"; 
	$commandline_output = $commandline_output."\nStep 1: Make Initial Ad Request to the Canary Cluster\n"; 
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://crtldev.aimatch.com:60000/canary/getid/mid=".$random."/external=admeld</b><br>"; 
	$commandline_output = $commandline_output."     http://crtldev.aimatch.com:60000/canary/getid/mid=".$random."/external=admeld\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 
    
	$url = "http://crtldev.aimatch.com:60000/canary/getid/mid=".$random."/external=admeld";
	$output1 = MakeAdCall($url,$cookiefile,$debug);
	$output_array1 = MakeOutputArray($output1,$debug);
	$first_response = FirstResponse($output_array1,"Location",$debug);
	$first_response_status = CheckFirstResponse($first_response,$web,$debug);
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response: </b>".$first_response."<br>"; 
	$commandline_output = $commandline_output."     Response: ".$first_response."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response Status: </b>".$first_response_status."</font><br><hr>"; 
	$commandline_output = $commandline_output."     Response Status: ".$first_response_status."\n\n\n"; 
	
//
// Step 2: Make Initial Request AdMeld
// http://tag.admeld.com/id?redirect=http://crtldev.aimatch.com:60000/canary/setid/external=ADMELD/value=[admeld_user_id]
//
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Step 2: Make Initial Request AdMeld</b></font><br>"; 
	$commandline_output = $commandline_output."Step 2: Make Initial Request AdMeld\n"; 
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$first_response."</b><br>"; 
	$commandline_output = $commandline_output."     ".$first_response."\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 
	
    $url2 = $first_response;
	$output2 = MakeAdCall($url2,$cookiefile,$debug);
	$output_array2 = MakeOutputArray($output2,$debug);
	list($second_response, $value) = SecondResponse($output_array2,"Location",$debug);
	$second_response_status = CheckSecondResponse($second_response,$value,$web,$debug);
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response: </b>".$second_response."<br>"; 
	$commandline_output = $commandline_output."     Response: ".$second_response."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AdMeld ID: </b>".$value."<br>"; 
	$commandline_output = $commandline_output."     AdMeld ID: ".$value."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response Status: </b>".$second_response_status."</font><br><hr>"; 
	$commandline_output = $commandline_output."     Response Status: ".$second_response_status."\n\n\n"; 

	
//
// Step 3: Make Cookie Request to aiMatch
// http://crtldev.aimatch.com:60000/canary/setid/external=ADMELD/value=[AdMeldID]
//
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Step 3: Make Cookie Request to aiMatch</b></font><br>"; 
	$commandline_output = $commandline_output."Step 3: Make Cookie Request to aiMatch\n"; 
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$second_response."</b><br>"; 
	$commandline_output = $commandline_output."     ".$second_response."\n"; 
	
	if (isset($web)) { $web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; }
	else { $commandline_output = $commandline_output."     -----\n"; }
    $url3 = $second_response;
	$output3 = MakeAdCall($url3,$cookiefile,$debug);
	$output_array3 = MakeOutputArray($output3,$debug);
	list($third_response, $third_response_value) = ThirdResponse($output_array3,"Set-Cookie",$debug);
	$third_response_status = CheckThirdResponse($third_response,$third_response_value,$web,$debug);

	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response (Set-Cookie): </b>".$third_response."<br>"; 
	$commandline_output = $commandline_output."     Response (Set-Cookie): ".$third_response."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AdMeld Cookie ID: </b>".$third_response_value."<br>"; 
	$commandline_output = $commandline_output."     AdMeld Cookie ID: ".$third_response_value."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response Status: </b>".$third_response_status."</font><br><hr>"; 
	$commandline_output = $commandline_output."     Response Status: ".$third_response_status."\n\n\n"; 

//
// Step 4: Test Basic aiMatch Cookie Functionality
// http://crtldev.aimatch.com:60000/canary/setid/mid=[random]1/external=CANARY/value=[testtime]
//
	$set_cookie_url = "http://crtldev.aimatch.com:60000/canary/setid/mid=".$random."1/external=CANARY/value=".$testtime;
	
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Step 4: Test Basic aiMatch Cookie Functionality</b></font><br>"; 
	$commandline_output = $commandline_output."Step 4: Test Basic aiMatch Cookie Functionality\n"; 

	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$set_cookie_url."</b><br>"; 
	$commandline_output = $commandline_output."     ".$set_cookie_url."\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 
	
    $output4 = MakeAdCall($set_cookie_url,$cookiefile,$debug);
	$output_array4 = MakeOutputArray($output4,$debug);
	list($fourth_response, $fourth_response_value) = FourthResponse($output_array4,"Set-Cookie",$debug);
	$fourth_response_status = CheckFourthResponse($fourth_response,$fourth_response_value,$web,$debug);

	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response (Set-Cookie): </b>".$fourth_response."<br>"; 
	$commandline_output = $commandline_output."     Response (Set-Cookie):  ".$fourth_response."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aiMatch CANARY Cookie: </b>".$fourth_response_value."<br>"; 
	$commandline_output = $commandline_output."     aiMatch CANARY Cookie: ".$fourth_response_value."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response Status: </b>".$fourth_response_status."</font><br><hr>"; 
	$commandline_output = $commandline_output."     Response Status: ".$fourth_response_status."\n\n\n"; 

//
// Step 5: Display the urldecoded contents of the cookie file and verify the contents
//
	$cookiefile_contents = urldecode(file_get_contents($cookiefile));
	
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Step 5: Display the urldecoded contents of the Cookie File and verify the contents</b></font><br>"; 
	$commandline_output = $commandline_output."Step 5: Display the urldecoded contents of the Cookie File and verify the contents\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 
	
	$web_output = $web_output."<pre>".$cookiefile_contents."</pre><br>"; 
	$commandline_output = $commandline_output."".$cookiefile_contents."\n"; 
	
	$cookiefile_status = CheckCookieFile($cookiefile,$value,$second_response,$third_response,$fourth_response,$web,$debug);
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cookie File Contents: </b>".$cookiefile_status."</font><br><hr>"; 
	$commandline_output = $commandline_output."     Cookie File Contents: ".$cookiefile_status."\n\n\n"; 

if ($first_response_status=="pass" && $second_response_status=="pass" && $third_response_status=="pass" && $fourth_response_status=="pass" && $cookiefile_status == "pass"){
	$web_status_output = $web_status_output."<font face=\"Tahoma\" size=\"3\" color=\"green\"><b>***** SUCCESS - AdMeld responses are valid and the cookie file has been validated</b></font><br><br><hr>"; 
	$commandline_status_output = $commandline_status_output."\n***** SUCCESS - AdMeld responses are valid and the cookie file has been validated\n\n"; 
	$status = "pass"; 
}
else {
	$web_status_output = $web_status_output."<font face=\"Tahoma\" size=\"3\" color=\"red\"><b>***** FAIL - AdMeld responses are not valid or the cookie file is not valid</b></font><br><br><hr>"; 
	$commandline_status_output = $commandline_status_output."\n***** FAIL - AdMeld responses are not valid or the cookie file is not valid\n\n"; 
	$status = "fail"; 
}

	if (isset($web)) { 
		echo $web_status_output; 
		echo $web_output; }
	else {
		echo $commandline_status_output;
		echo $commandline_output; }

unlink($cookiefile);

$filestatus = WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$commandline_status_output,$commandline_output,$output);

if (isset($debug)) { phpinfo(); }

//================= Functions =================
	
function WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$commandline_status_output,$commandline_output,$output){
	// Setup the archival file name for the test
	$datestamp = date("YmdGis");
	$directory = "/http/matchtest/archived_results/admeldtest/";
	if(!is_dir($directory)){
		if(!mkdir($directory, 0777)) {
			die('Failed to create directory: '.$directory);
		}
	}

	// Write out the information to the testresults.txt file.
	$lines = file('/http/matchtest/testresults.txt');
	$numitems = count($lines);

	// Loop through our array, show HTML source as HTML source; and line numbers too.

	if (isset($debug)) { echo "<br><pre>HTTP Response Output:<br><br>"; print_r($lines); echo "</pre><br>"; }

	$newfile = $lines[0];
	$i=1;
	while($i <= $numitems) {
		$pieces = explode("|",$lines[$i]);
		if($testname == $pieces[1]){
			$replaced_line = $status."|".$testname."|".$testdesc."|".$testurl."|".$testtime."\n";
			$newfile = $newfile.$replaced_line;
		}
		else{
			$newfile = $newfile.$lines[$i];
		}
		$i++;
	}

	if (isset($debug)) { echo "<br><pre>New File Output:<br><br>"; print_r($newfile); echo "</pre><br>"; }

	$fp = fopen('/http/matchtest/testresults.txt', 'w');

	fwrite($fp, $newfile);
	fclose($fp);

// Write out results of the test to a file 
	if (isset($web)) { $testfilename = $directory.$testshortname.".web.".$status.".".$datestamp.".".$testtime.".txt"; }
	else { $testfilename = $directory.$testshortname.".commandline.".$status.".".$datestamp.".".$testtime.".txt"; }

	if (!$fp1 = fopen($testfilename,'w')) {
		echo "Cannot open file ($testfilename)";
		exit;
	}

	fwrite ($fp1,$commandline_status_output."\n\n");
	fwrite ($fp1,$commandline_output."\n\n");
//	fwrite ($fp1,"\n\n".$message3."\n\n".$output."\n\n");
	fclose($fp1);

	return 0;
}

function MakeAdCall($url,$cookiefile,$debug) {
	$UserAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";

    if (isset($debug)) { echo "URL: ".$url."<br>"; }

    // initilize the curl call
    $ch = curl_init();
    // set useragent
    curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
	// Read Cookies from Cookiefile
	curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile); 
    // Set curl to use $url for http call
    curl_setopt($ch, CURLOPT_URL, $url);
    // show client headers
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // have curl put X-Forwarded-For into headers
    curl_setopt($ch,CURLOPT_HTTPHEADER, $client_headers);
    // store results as the return of curl_exec
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);		
    // if HTML error 400 over over, fail
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
	// Save Cookies in Cookiefile
	curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile); 

    $output = curl_exec($ch);
    if (isset($debug)) { echo "HTTP Response Output:<br><pre>"; print_r($output); echo "</pre><br>"; }
    //close curl connection
    curl_close($ch);

return $output;
}


function MakeOutputArray($output,$debug){
        $output_array = preg_split('/[\n]/', $output, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        if (isset($debug)) { echo "HTTP Response Output (array):<br><pre>"; print_r($output_array); echo "</pre><br><br>"; }

return $output_array;
}


function FirstResponse($output_array,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output_array));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string: <pre>".$string."</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output_array); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], " ")){
			$explode_a = explode(" ", trim($line_match[0]));
			if (isset($debug)) { echo "\n\nRedirect To: ".$explode_a[1]."<br>"; }
			$result = $explode_a[1]; 
		}
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return $result;
}

function CheckFirstResponse($first_response,$web,$debug){
    if (isset($debug)) { echo "<br>Response Status: ".$first_response."<br>"; }

		if(strcasecmp(trim($first_response), "http://tag.admeld.com/id?redirect=http://crtldev.aimatch.com:60000/canary/setid/external=ADMELD/value=[admeld_user_id]")==0){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}

function SecondResponse($output_array,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output_array));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string: <pre>".$string."</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output_array); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], " ")){
			$explode_a = explode(" ", trim($line_match[0]));
			if (isset($debug)) { echo "<br>Redirect To: ".$explode_a[1]."<br>"; }
			$explode_b = explode("value=", trim($explode_a[1]));
			if (isset($debug)) { echo "<br>Value: ".$explode_b[1]."<br>"; }
			
			$value = $explode_b[1];
			$result = $explode_a[1]; 
		}
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return array($result,$value);
}

function CheckSecondResponse($second_response,$value,$web,$debug){
    if (isset($debug)) { echo "<br>Response Status: ".$second_response."<br>"; }
		$string = "http://crtldev.aimatch.com:60000/canary/setid/external=ADMELD/value=".$value;
    if (isset($debug)) { echo "<br>Check Value: ".$string."<br>"; }
		
		if(strcasecmp(trim($second_response), trim($string))==0){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}

function ThirdResponse($output_array,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output_array));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string: <pre>".$string."</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output_array); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], " ")){
			$explode_a = explode(" ", trim($line_match[0]));
			if (isset($debug)) { echo "<br>Explode_a: ".$explode_a[1]."<br>"; }
			$explode_b = explode("External=", trim($explode_a[1]));
			if (isset($debug)) { echo "<br>Cookie [External] Value: ".$explode_b[1]."<br>"; }
			$explode_c0 = explode("%2FADMELD%3D", trim($explode_b[1]));
			$explode_c = str_replace(";","",$explode_c0[1]);
			if (isset($debug)) { echo "<br>Value: ".$explode_c."<br>"; }
			
			
			$value = $explode_c;
			$result = $explode_b[1]; 
		}
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return array($result,$value);
}

function CheckThirdResponse($third_response,$third_response_value,$web,$debug){
    if (isset($debug)) { echo "<br>Response Status: ".$third_response."<br>"; }
		$string = "%2FADMELD%3D".$third_response_value.";";
    if (isset($debug)) { echo "<br>Check Value: ".$string."<br>"; }
		
		if(strcasecmp(trim($third_response), trim($string))==0){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}

function FourthResponse($output_array,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output_array));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string: <pre>".$string."</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output_array); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], " ")){
			$explode_a = explode(" ", trim($line_match[0]));
			if (isset($debug)) { echo "<br>Explode_a: ".$explode_a[1]."<br>"; }
			$explode_b = explode("External=", trim($explode_a[1]));
			if (isset($debug)) { echo "<br>Cookie [External] Value: ".$explode_b[1]."<br>"; }
			$explode_c0 = explode("%2FCANARY%3D", trim($explode_b[1]));
			$explode_c = str_replace(";","",$explode_c0[1]);
			if (isset($debug)) { echo "<br>Value: ".$explode_c."<br>"; }
			
			
			$value = $explode_c;
			$result = "%2FCANARY%3D".$explode_c.";"; 
		}
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return array($result,$value);
}

function CheckFourthResponse($fourth_response,$fourth_response_value,$web,$debug){
    if (isset($debug)) { echo "<br>Fourth Response: ".$fourth_response."<br>"; }
    if (isset($debug)) { echo "<br>Fourth Response Value: ".$fourth_response_value."<br>"; }
		$string = "%2FCANARY%3D".$fourth_response_value.";";
    if (isset($debug)) { echo "<br>Check Value: ".$string."<br>"; }
		
		if(strcasecmp(trim($fourth_response), trim($string))==0){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}

function CheckCookieFile($cookiefile,$value,$second_response,$third_response,$fourth_response,$web,$debug){
	$line = file($cookiefile);
	if (isset($debug)) { echo "Value: ".$value."<br>"; }
	if (isset($debug)) { echo "Second Response: ".$second_response."<br>"; }
	if (isset($debug)) { echo "Third Response: ".$third_response."<br>"; }
	if (isset($debug)) { echo "Fourth Response: ".$fourth_response."<br>"; }
	if (isset($debug)) { echo "Cookie File Contents (array):<br><pre>"; print_r($line); echo "</pre><br>"; }

	$admeld_pieces = explode("\t",$line[4]);
	if (isset($debug)) { echo "Cookie File AdMeld Line Contents (array):<br><pre>"; print_r($admeld_pieces); echo "</pre><br>"; }
	if (trim($admeld_pieces[0]) == ".tag.admeld.com" && trim($admeld_pieces[5]) == "meld_sess" && trim($admeld_pieces[6]) == trim($value)) {
		$admeld_status = "pass"; }
	else {
		$admeld_status = "fail"; }
	if (isset($debug)) { echo "AdMeld Status: ".$admeld_status."<br>"; }

	$aimatch_pieces = explode("\t",$line[5]);
	if (isset($debug)) { echo "Cookie File aiMatch Line Contents (array):<br><pre>"; print_r($aimatch_pieces); echo "</pre><br>"; }

	$aimatch_cookie_value = str_replace(";","",$third_response).str_replace(";","",$fourth_response);
	if (isset($debug)) { echo "aiMatch Cookie Value: ".$aimatch_cookie_value."<br>"; }
	
	if (trim($aimatch_pieces[0]) == ".aimatch.com" && trim($aimatch_pieces[5]) == "External" && trim($aimatch_pieces[6]) == trim($aimatch_cookie_value)) {
		$aimatch_status = "pass"; }
	else {
		$aimatch_status = "fail"; }
	if (isset($debug)) { echo "aiMatch Status: ".$aimatch_status."<br>"; }

	if ($aimatch_status == "pass" && $admeld_status == "pass"){
		$status = "pass"; }
	else {
		$status = "fail"; }

	if (isset($debug)) { echo "Cookie File Status: ".$aimatch_status."<br>"; }
		
return $status;
}

?>
